class PartnerCategoriesController < ApplicationController
  before_action :set_partner_category, only: [:show, :edit, :update, :destroy]
  before_action :set_page_title

  # GET /partner_categories
  # GET /partner_categories.json
  def index
    @partner_categories = PartnerCategory.where(build_search(search_params)).order(created_at: :desc).page(params[:page])
  end

  # GET /partner_categories/1
  # GET /partner_categories/1.json
  def show
  end

  # GET /partner_categories/new
  def new
    @partner_category = PartnerCategory.new
  end

  # GET /partner_categories/1/edit
  def edit
  end

  # POST /partner_categories
  # POST /partner_categories.json
  def create
    @partner_category = PartnerCategory.new(partner_category_params)

    respond_to do |format|
      if @partner_category.save
        format.html { redirect_to partner_categories_path, notice: '往来单位分类创建成功。' }
      else
        format.html { render :new }
      end
    end
  end

  # PATCH/PUT /partner_categories/1
  # PATCH/PUT /partner_categories/1.json
  def update
    respond_to do |format|
      if @partner_category.update(partner_category_params)
        format.html { redirect_to @partner_category, notice: 'Partner category was successfully updated.' }
        format.json { render :show, status: :ok, location: @partner_category }
      else
        format.html { render :edit }
        format.json { render json: @partner_category.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /partner_categories/1
  # DELETE /partner_categories/1.json
  def destroy
    @partner_category.destroy
    respond_to do |format|
      format.html { redirect_to partner_categories_url, notice: 'Partner category was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_partner_category
      @partner_category = PartnerCategory.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def partner_category_params
      params.require(:partner_category).permit(:code, :name, :parent_id)
    end

    def set_page_title
      @page_title = "往来单位"
    end

    def search_params
      if params[:partner_category].present?
        partner_category_params.permit(:code, :name)
      end
    end
end
